<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shelves extends MY_Controller {
	function __construct(){
			parent::__construct();
			$this->school_id = $this->user_data['school_id'];
			$this->teacher_id = $this->user_data['teacher_id'];
			$this->uid = $this->user_data['id'];
			$this->load->model('videos');
		}
	/**
	 * 视频课
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index($status = 10,$sort = 0,$asc_des = 0,$search = 0,$cate_id=0, $sj=1, $limit = 0)
	{
		$this->load->model('Shelvess');
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['status']) ? $post_data['status'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
			$sj = (isset($post_data['sj'])) ? $post_data['sj'] : 1;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 9;
		}
		//获取分销权限；
		// $system_course = $this->Shelvess->get_system_course_privilege();
		// if(empty($system_course)){
		// 	echo "暂时没有上架系统课程的权限";exit;
		// }else{
		// 	if(count($system_course) > 1){
		// 		foreach ($system_course as $key => $value) {
		// 			$temp[] = $value['category_id'];
		// 		}
		// 		$category_id = implode(',',$temp);
		// 	}else{
		// 		$category_id = $system_course[0]['category_id'];
		// 	}
		// }
		$this->load->model("Courses");
		$data = array( 'status' => $status, 'cate_id'=>$cate_id, 'type' => 1, 'sort' => $sort,'asc_des' => $asc_des, 'sj'=> $sj, 'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 10;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->Shelvess->get_list_ofpage($data['limit'], $offset, $data);
		foreach ($result['result'] as $key => $value) {
			$nouse_code = $this->Shelvess->get_code($value['id'],1);
			$result['result'][$key]['nouse_code'] = $nouse_code;
			$use_code = $this->Shelvess->get_code($value['id'],2);
			$result['result'][$key]['use_code'] = $use_code;
			$dongjie = get_field_by_where("o2o_school_system_video_course_codes", "count(id) AS count", "use_uid <= 0 AND dongjie=1 AND course_id = '{$value['id']}' AND school_id = '{$this->user_data['school_id']}'");
			$result['result'][$key]['dongjie'] = $dongjie['count'];
		}
		
		// $subject = $this->Courses->get_cate();
		$this->load->library('pagination');
		$page_config['base_url'] = '/shelves/index/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'].'/'.$sj;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$cate = $this->Courses->get_system_cate();
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("zhiboke", $result['result']);
		$this->smarty->assign("pagination", $pagination);
		// $this->smarty->assign("subject", $subject);
		$this->smarty->assign("menu", "shelves");
		$this->smarty->display("shelves/index.html");
	}

	function sj(){
		$post_data = $this->input->post();
		$this->load->model("Courses");
		$this->load->model("Shelvess");
		$check = $this->Courses->check_is_sj($post_data['id'],$this->school_id);
		var_dump($check);
		if($post_data){
			if($check){
				$this->shangjia($post_data['id']);
			}else{
				$result = $this->Shelvess->get_fx_course_info($post_data['id']);
				if(!isset($post_data['is_free'])){
					$post_data['is_free'] = 0;
				}
				$data['uid'] = $result['uid'];
				$data['is_free'] = $post_data['is_free'];
				$data['cate_id'] = $post_data['cate_id'];
				$data['course_id'] = $post_data['id'];
				$data['teacher_id'] = $result['teacher_id'];
				$data['fx_price'] = $post_data['fx_price'];
				$data['commission'] = $post_data['commission'];
				if($result['school_id'] == 0){
					$data['is_fenxiao'] = 1;
				}
				$data['school_id'] = $this->school_id;
				$data['type'] = $result['type'];
				$result = $this->db->insert('course_shelves',$data);
				if($result){
					redirect('/shelves/index/');
				}
			}
			
			
		}
	}

	function shangjia($id){
		$this->db->where('course_id',$id);
		$result = $this->db->delete('course_shelves');
		if($result){
			echo 2;exit;
		}else{
			echo 0;exit;
		}
	}


	function shouquan($id = 0, $type = 1){
		$post_data = $this->input->post();
		if($post_data){
			$data['data_id'] = $id;
			$data['school_id'] = $this->school_id;
			$data['num'] = $post_data['num'];
			$data['apply_time'] = time();
			$data['data_type'] = $type;
			$data['type'] = 2;
			$data['reviewed'] = 0;
			$data['remarks'] = $post_data['remarks'];
			$result = $this->db->insert('o2o_school_order_records',$data);
			if($result){
				show_message($_SERVER['HTTP_REFERER'],'申请成功');
			}else{
				show_message($_SERVER['HTTP_REFERER'],'操作失败');
			}
		}else{
			$this->smarty->assign("type", $type);
			$this->smarty->display("shelves/add_shouquan.html");
		}
	}

	function up($course_id = 0, $pack_id = 0){
		$post_data = $this->input->post();
		if($post_data){
			$course_info = get_field_by_where("course", "*", "id='{$post_data['course_id']}'");
			if(!$course_info){
				show_message($_SERVER['HTTP_REFERER'], "科目不存在");
			}else{
				$where = "school_id='{$this->user_data['school_id']}' AND course_id='{$post_data['course_id']}'";
				$shelves_info = get_field_by_where("course_shelves", "id", $where);
				if($shelves_info){
					// echo json_encode(array("code" => 0));
					$post_data['status'] = 1;
					$this->db->where("id", $shelves_info['id']);
					$this->db->update("course_shelves", $post_data);
					echo json_encode(array("code" => 2));
				}else{
					/*$shelves_data['tk_category_id'] = $subject_info['tk_category_id'];
					$shelves_data['course_id'] = $post_data['course_id'];
					$shelves_data['type'] = ($subject_info['school_id'] == 1) ? 1 : 2;
					$shelves_data['school_id'] = $this->user_data['school_id'];
					$shelves_data['price'] = $post_data['price'];
					$shelves_data['status'] = 1;
					$shelves_data['commission'] = $post_data['commission'];
					$shelves_data['expire_day'] = $post_data['expire_day'];
					$shelves_data['on_time'] = time();*/
					$this->load->model("Shelvess");
					$result = $this->Shelvess->get_fx_course_info($post_data['course_id']);
					$data['uid'] = $result['uid'];
					$data['is_free'] = $post_data['is_free'];
					$data['cate_id'] = $post_data['cate_id'];
					$data['course_id'] = $post_data['course_id'];
					$data['teacher_id'] = $result['teacher_id'];
					$data['fx_price'] = $post_data['fx_price'];
					$data['expire_day'] = $post_data['expire_day'];
					$data['commission'] = $post_data['commission'];
					if($result['school_id'] == 0){
						$data['is_fenxiao'] = 1;
					}
					$data['school_id'] = $this->school_id;
					$data['type'] = $result['type'];
					$this->db->insert("course_shelves", $data);
					echo json_encode(array("code" => 1));
				}
			}
		}else{
			if($pack_id > 0){
				$pack_info = get_field_by_where("course_shelves", "*", "id='{$pack_id}'");
				$this->smarty->assign("pack_info", $pack_info);
			}
			$code_info = get_field_by_where("o2o_school_system_video_course_codes", "*", "school_id='{$this->user_data['school_id']}' AND course_id='{$course_id}' AND use_uid <= 0 AND status = 1 ORDER BY expire_day DESC LIMIT 1");
			$this->smarty->assign("code_info", $code_info);
			$school_order_info = get_field_by_where("o2o_school_order_records", "*", "school_id='{$this->user_data['school_id']}' AND data_id='{$course_id}' AND data_type = 2 AND reviewed = 1 ORDER BY id DESC");
			$this->smarty->assign("school_order_info", $school_order_info);
			$this->load->model("Courses");
			$cate = $this->Courses->get_school_cate();
			$this->smarty->assign("cate", $cate);
			$this->smarty->assign("action", '授权');
			$this->smarty->assign("course_id", $course_id);
			$this->smarty->assign("pack_id", $pack_id);
			$this->smarty->display("shelves/shelves.html");
		}
	}
}
